package livingarea.actions_impl;

import livingarea.actions.EatingType;
import livingarea.Members.Beast;
import livingarea.Members.AreaMember;

/**
 * Class incapsulate
 * <code> eat() </code> method and realized eating pray action.
 *
 * @author Muzalevskiy Vitaliy
 */
public class EatPray implements EatingType {

    /**
     * <code> Beast </code> object that delegate object of current class.
     */
    private Beast predator;

    /**
     * Constructor for current class.
     *
     * @param predator - <code> Beast </code> object that delegate object of
     * current class.
     */
    public EatPray(Beast predator) {
        this.predator = predator;
    }

    /**
     * Current method realized eating pray action.
     */
    @Override
    public void eat() {
        for (int i = 0; i < predator.getLivingArea().getAreaMembersCount(); i++) {
            AreaMember areaMember = predator.getLivingArea().getAreaMember(i);
            if ((areaMember.isPray())
                    && (areaMember.getPosition().equals(predator.getPosition()))) {
                if (deleteIfExist(areaMember)) {
                    return;
                }
            }
        }
    }

    private boolean deleteIfExist(AreaMember intermidiateFish) {
        if (!predator.getLivingArea().memberIsDead(intermidiateFish)) {
            predator.getLivingArea().removeMember(intermidiateFish);
            predator.increaseEatCount();
            predator.getLivingArea().getStatistics().decreasePrayCount();
            predator.getLivingArea().getStatistics().increaseEatCount();
            return true;
        }
        return false;
//        else {
//            predator.increaseEatCount();
//            System.out.println("Predator caught a pray");
//            predator.getLivingArea().getStatistics().increaseEatCount();
//        }
    }
}
